package com.itstorm.pomeloweather.ui;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.itstorm.pomeloweather.R;
import com.itstorm.pomeloweather.util.BroadcastCode;
import com.itstorm.pomeloweather.util.ImageUtils;

public class AreasListAdapter extends BaseAdapter {

	private Map<Integer, View> rowViews = new HashMap<Integer, View>();
	private String[] areas;
	private Context context;
	private SharedPreferences weatherList;
	private SharedPreferences.Editor editor;
	private Display display;

	public AreasListAdapter(Context context, String[] mAreas, Display mDisplay) {
		this.context = context;
		this.areas = mAreas;
		this.display = mDisplay;
		weatherList = context.getSharedPreferences("WeatherList", 0);
		editor = weatherList.edit();
	}

	public void changeList(String[] mAreas) {
		this.areas = mAreas;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return areas.length;
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return areas[position];
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = rowViews.get(position);
		if (rowView == null) {
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			rowView = layoutInflater.inflate(R.layout.areaslist_item, null);

			final int mPosition = position;
			FrameLayout flAreas = (FrameLayout) rowView
					.findViewById(R.id.fl_areas);
			TextView tvAreas = (TextView) rowView.findViewById(R.id.tv_areas);
			tvAreas.setText(areas[mPosition]);

			flAreas.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

					if (areas.length < 2) {
						ImageUtils.showToast(context, "至少需要一个地点!", 1200,
								display.getWidth());
						Log.d("JSON",
								"---------------1 only one city---------------\n"
										+ areas.toString());
					} else {
						final int delPosition = mPosition;
						Log.d("JSON",
								"---------------2 remove city---------------\n"
										+ areas[mPosition].toString());
						editor.remove(areas[mPosition]);
						editor.commit();
						String newAreas = "";
						for (int i = 0; i < areas.length; i++) {
							if (!areas[i].equals(areas[mPosition])) {
								newAreas = newAreas + areas[i] + ",";
							}
						}
						if (newAreas.endsWith(",")) {
							newAreas = newAreas.substring(0,
									newAreas.length() - 1);
						}
						Log.d("JSON",
								"---------------3 new city---------------\n"
										+ newAreas.toString());
						editor.putString("Areas", newAreas);
						editor.commit();

						Intent mIntent = new Intent();
						mIntent.setAction(BroadcastCode.changeAreas);
						mIntent.putExtra("type", BroadcastCode.delArea);
						Log.d("JSON",
								"---------------4 del Position---------------\n"
										+ delPosition);
						mIntent.putExtra("index", delPosition);
						context.sendBroadcast(mIntent);
					}

				}
			});

		}

		return rowView;
	}
}
